<template>
<div>
<template>

<!-- 添加老师隐藏端口 -->
<el-dialog title="添加老师" :visible.sync="dialogFormVisible">
  <el-form :model="teacher">
    <el-form-item label="老师姓名" label-width="120px">
      <el-input v-model="teacher.username" autocomplete="off"></el-input>
    </el-form-item> 
    <el-form-item label="老师密码" label-width="120px">
      <el-input  v-model="teacher.password" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="所属学院" label-width="120px">
      <el-select v-model="teacher.departmentId" placeholder="请选择">
        <el-option
          v-for="item in departments"
          :key="item.id"
          :label="item.departmentName"
          :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="教学班级" label-width="120px">
      <el-select v-model="teacher.classIds" placeholder="请选择" multiple>
        <el-option
          v-for="item in classes"
          :key="item.id"
          :label="item.className"
          :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
      <el-form-item label="老师号" label-width="120px">
      <el-input v-model="teacher.teahcerId" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label-width="200px">
      <el-button size="small"  type="primary" @click="ensureAddTeacher()">添加</el-button>
      <el-button size="small"  type="primary" @click="cancelAddTeacher()">取消</el-button>
    </el-form-item>
  </el-form>
  </el-dialog>

  <!-- 修改老师隐藏端口 -->
    <el-dialog title="修改老师" :visible.sync="updateDialogFormVisible">
  <el-form :model="uTeacher">
    <el-form-item label="老师姓名" label-width="120px">
      <el-input v-model="uTeacher.username" autocomplete="off"></el-input>
    </el-form-item> 
    <el-form-item label="老师密码" label-width="120px">
      <el-input  v-model="uTeacher.password" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="所属学院" label-width="120px">
      <el-select v-model="uTeacher.departmentId" placeholder="请选择">
        <el-option
          v-for="item in departments"
          :key="item.id"
          :label="item.departmentName"
          :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="教学班级" label-width="120px">
      <el-select v-model="uTeacher.classIds" placeholder="请选择" multiple>
        <el-option
          v-for="item in classes"
          :key="item.id"
          :label="item.className"
          :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
      <el-form-item label="老师号" label-width="120px">
      <el-input v-model="uTeacher.teahcerId" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label-width="200px">
      <el-button size="small"  type="primary" @click="ensureUpdateTeacher()">修改</el-button>
      <el-button size="small"  type="primary" @click="cancelUpdateTeacher()">取消</el-button>
    </el-form-item>
  </el-form>
  </el-dialog>

<!-- 教师排课端口 -->
<el-dialog title="教师排课" :visible.sync="classDialogFormVisible">
  <el-form :model="relation">
    <!-- 授课老师 -->
    <el-form-item label="授课老师" label-width="120px">
      <el-select v-model="relation.teacherId" placeholder="请选择老师">
        <el-option
          v-for="item in teachers"
          :key="item.id"
          :label="item.username"
          :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
      <!-- 课程信息 -->
      <el-form-item label="课程信息" label-width="120px">
      <el-select v-model="relation.courseId" placeholder="请选择课程">
        <el-option
          v-for="item in courses"
          :key="item.id"
          :label="item.courseName"
          :value="item.id">
        </el-option>
      </el-select>
      </el-form-item>
    <el-form-item label-width="200px">
      <el-button size="small"  type="primary" @click="ensureClass()">排课</el-button>
      <el-button size="small"  type="primary" @click="cancelClass()">取消</el-button>
    </el-form-item>
  </el-form>
  </el-dialog>

  <!-- 页面主体 -->
   <el-row :gutter="0">

     <!-- 添加老师按钮 -->
    <el-col :span="2">
      <el-button size="small"  type="primary" @click="addTeacher()">增加老师</el-button>
    </el-col>

        <el-col :span="2">
      <el-button size="small"  type="primary" @click="addClass()">教师排课</el-button>
    </el-col>

    <!-- 输入框 -->
    <el-col :span="6">
        <el-input
        v-model="teacherDTO.username"
        class="w-50 m-2"
        size="small"
        placeholder="请输入姓名"
        @input="search()"
        />
    </el-col>

    <!-- 搜索按钮 -->
    <el-col :span="3">
      <el-button size="small"  type="primary" @click="search()">搜索</el-button>
    </el-col>
  </el-row>

  <!-- 表格 -->
  <el-table :data="teachers" stripe border height="300">
    <el-table-column prop="id" sortable=true label="ID"/>
    <el-table-column prop="username" sortable=true label="昵称"/>
    <el-table-column prop="teahcerId" sortable=true label="老师工号" />
    <el-table-column prop="departmentName" sortable=true label="所属学院" />
        <el-table-column label="操作">
            <template #default="scope">
                <el-button size="small" @click="updateTeacher(scope.row)">
                   修改
                </el-button>
                <el-button size="small" type="danger" @click="deleteTeacher(scope.row.id)">
                   删除
                </el-button>
            </template>
            </el-table-column>
  </el-table>

  <!-- 分页部分 -->
  <el-pagination
    small
    background
    layout="prev, pager, next"
    :page-size="teacherDTO.capacity"
    :current-page.sync="teacherDTO.index"
    @current-change="changeCurrent"
    :total="count"
    class="mt-4"
  />
</template>

</div>
</template>
<script>
export default {
  name: 'Home',
  data () {
    return {
      // 当前登录的用户角色
      role: '老师',
      // 表格映射对象
      teachers: [],
      // 修改的对象
      uTeacher: {
        username: '',
        password: '',
        roleId: '',
        teacherNo: ''
      },
      // 表格查询对象
      teacherDTO: {
          username: ''
      },
      relation: {},
      courses: [],
      // 角色列表
      roles: [],
      // 添加老师弹出控制
      dialogFormVisible: false,
      // 修改老师弹出控制
      updateDialogFormVisible: false,
      // 添加老师实体
      teacher: {
          username: '',
          password: '',
          roleId: '',
          teacherNo: ''
      },
      // 老师列表总数
      count: 0,
      classIds: [],
      departments: [],
      classes: [],
      classDialogFormVisible: false
    }
  },
  // 页面初始化函数 
  created () {
    this.$http({
        url: "/teacher",
        params: {
            username: this.teacherDTO.username
        }
    }).then(response => {
      console.log(response.data)
       this.teachers = response.data
    })
    this.$http({
        url: "/classes",
        params: {
            classesName:''
        }
    }).then(response => {
       this.classes = response.data
    })
    this.$http({
        url: "/department",
        params: {
            departmentName:''
        }
    }).then(response => {
       this.departments = response.data
    })
    this.$http({
        url: "/course",
        params: {
            courseName:''
        }
    }).then(response => {
       this.courses = response.data
    })
  },
  methods: {
    // 改变当前页
    changeCurrent() {
      findAll()
    },
    // 添加老师
     addTeacher() {
       this.dialogFormVisible=true;
     },
     updateTeacher(data) {
       this.uTeacher=data
       this.updateDialogFormVisible=true
     },
     deleteTeacher(id) {
        this.$http.delete('/teacher/' + id).then(response => {
          this.$message({
          message: '删除老师成功',
          type: 'success'
          });
          this.findAll()
        })
     },
     addClass() {
       this.classDialogFormVisible = true;
     },
     ensureClass() {
       this.$http.post('/relation', this.relation).then(response => {
         this.$message({
           message: '排课成功',
           type: 'success'
         })
         this.classDialogFormVisible = false
       })
     },
     cancelClass() {
         this.classDialogFormVisible = false
     },
     ensureAddTeacher() {
      this.teacher.classIds = JSON.stringify(this.teacher.classIds)
      this.$http.post("/teacher", this.teacher).then(response => {
        this.$message({
          message: '添加老师成功',
          type: 'success'
        });
        this.clearTeacher()
        this.findAll()
      })
    },
    // 取消添加老师
    cancelAddTeacher() {
      this.clearTeacher()
    },
    ensureUpdateTeacher() {
      this.uTeacher.classIds = JSON.stringify(this.uTeacher.classIds)
      this.$http.put('/teacher', this.uTeacher).then(response => {
        this.$message({
          message: '修改老师成功',
          type: 'success'
        });   
        this.updateDialogFormVisible=false
        this.findAll()
      })
    },
    cancelUpdatedTeacher() {
      this.updateDialogFormVisible=false
    },
    // 搜索对象，并映射
    search() {
      this.findAll()
    },
    // 查询所有，并映射
    findAll() {
      this.$http({
          url: "/teacher",
          params: {
              username: this.teacherDTO.username
          }
      }).then(response => {
        this.teachers = response.data
      })
    },
    // 清除老师，并关闭窗口
    clearTeacher() {
        this.teacher.username=''
        this.teacher.password=''
        this.teacher.classIds=[]
        this.teacher.teahcerId=''
        this.teacher.departmentId=''
        this.dialogFormVisible=false
     }
  }
}
</script>
<style>

</style>